<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>菜品分类列表-做法</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="css/font.css">
    <link rel="stylesheet" href="css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/xadmin.js"></script>
    <script type="text/javascript" src="js/cookie.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
			<span class="layui-breadcrumb">
				<a href="">首页</a>
				<a href="">菜品分类</a>
				<a>
					<cite>菜品分类列表-做法</cite></a>
			</span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);"
       title="刷新">
        <i class="layui-icon" style="line-height:30px">&#xe9aa;</i></a>
</div>
<div class="x-body">
	<!-- 表格 -->
    <table id="table_id" lay-filter="dev"></table>

</div>
<!-- th工具栏 -->
<script type="text/html" id="th_toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"><i class="layui-icon">&#xe640;</i>批量删除
        </button>
        <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon">&#xe608;</i>添加</button>
    </div>
</script>
<!-- tr工具条 -->
<script type="text/html" id="tr_toolbar">
    <a class="a_edit" title="编辑" onclick="" lay-event="edit"><i class="layui-icon">&#xe642;</i></a>
    <a class="a_delete" title="删除" onclick="" href="javascript:;" lay-event="delete"><i class="layui-icon">&#xe640;</i></a>
</script>

<script>
    layui.use('table', function (table) {
        // 表格数据加载
        var tableIns = table.render({
            id: 'layui_table_id',
            elem: '#table_id',
            toolbar: '#th_toolbar',
            defaultToolbar: ['filter', 'print'],
            // height: 320,
            url: '/sell/showFoodCookCategory',
            initSort: {
                field: 'categoryCreateTime',
                type: 'desc'
            },
            cols: [
                [{
                    type: 'checkbox'
                },
                    /*{
                        field: 'categoryId',
                        title: 'ID'
                    },*/
                    {
                        field: 'categoryName',
                        title: '类名'
                    },
                    {
                        field: 'categoryCreateTime',
                        title: '创建时间',
                        sort: true
                    },
					{
					    field: 'categoryModifiedTime',
					    title: '修改时间',
					},
                    {
                        title: '操作',
                        toolbar: '#tr_toolbar'
                    }
                ]
            ]
        });


        // 监听表头工具栏事件
        table.on('toolbar(dev)', function (obj) {
            // 获取选中行
            // var checkStatus = table.checkStatus(obj.config.id)

            // 选中行的数据
            var data = JSON.stringify(ids)

            switch (obj.event) {
                case 'add': // 添加事件
                    // layer.msg('add');
                    x_admin_show('添加分类', '/sell/cookCategoryAdd')
                    break;
                case 'delete': // 删除事件
                    // 异步删除
                    layer.confirm('确认要删除吗？', function () {
                        $.ajax({
                            url: '/sell/deleteMultiFoodCookCategory',
                            type: 'delete',
                            contentType: 'application/json;charset=utf-8',
                            data: data,
                            success: function (res) {
                                console.log(res)
                                if (res == 1) {
                                    layer.msg("删除成功");
                                    tableIns.reload({
                                        url: '/sell/showFoodCookCategory',
                                        page:{
                                            curr:1
                                        }
                                    })
                                } else {
                                    layer.msg("删除失败，请重试！")
                                }
                            }
                        });
                    });
                    break;
            }
        })

        // 监听表格操作
        table.on('tool(dev)', function (obj) {
            // console.log(obj)
            var data = obj.data; // 获取当前行数据
            var layEvent = obj.event; // 获取lay-event对应的值

            // console.log(data);

            // 工具栏事件监听
            if (layEvent === 'edit') {
                // 更新用户信息
                x_admin_show('编辑', '/sell/cookCategoryEdit?cookCategoryId=' + data.categoryId)
            } else if (layEvent === 'delete') {
                // 删除用户信息
                layer.confirm('确认要删除吗？', function () {

                    //发异步删除数据
                    $.ajax({
                        url: '/sell/deleteOneFoodCookCategory',
                        data: {categoryId: data.categoryId},
                        type: 'delete',
                        success: function (res) {
                            if (res == 1) {
                                obj.del();
                                layer.msg('已删除!', {
                                    icon: 1,
                                    time: 1000
                                });
                            } else {
                                layer.msg('删除失败！请重试！')
                            }
                        }
                    });

                });
            }
        })

        var ids = new Array()
        // 监听复选框
        table.on('checkbox(dev)', function (obj) {

            ids=[]
            var checkStatus = table.checkStatus('layui_table_id');
            var objs=checkStatus.data
            // console.log(objs)
            for (var i=0,len=objs.length;i<len;i++){
                ids.push(objs[i].categoryId)
            }

        })

    });

</script>

</body>

</html>
